package dasregistry.utils;

import java.util.Date;
import java.util.List;

import org.biojava.dasobert.dasregistry.Das1Source;
import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
import org.biojava.dasobert.dasregistry.DasSource;

import org.biojava.services.das2.DasSourceProvider;

/**
 * to store some of the registry responses or objects
 * 
 * @author jw12
 * 
 */
public class SingletonRegistryCache {
	DasSourceProvider das1Provider;
	List<DasSource> das1sources;
	long timeGetLastModifiedChecked = 0;
	List<DasCoordinateSystem> coordinateSystems;
	Date lastUpdateOfSources;// last time data was updated
	Date lastUpdateOfCoords;// last time data was updated

	public DasSourceProvider getDas1Provider() {
		return das1Provider;
	}

	public void setDas1Provider(DasSourceProvider das1Provider) {
		this.das1Provider = das1Provider;
	}

	public SingletonRegistryCache() {
		System.out.println("singletonCache created" + this.toString());

	}

	public List<DasSource> getDas1Sources() {
		
		Date dateLastModified = getLastModified();

		if (das1sources == null || das1sources.size() == 0
				|| lastUpdateOfSources == null
				|| dateLastModified.after(lastUpdateOfSources)) {
			refreshDasSources();
		}
		return das1sources;
	}

	private void refreshDasSources() {
		System.out.println("updating sources in cache");
		das1sources = das1Provider.getDasSources();
		lastUpdateOfSources = new Date(System.currentTimeMillis());
	}

	/**
	 * only make requests to database for this every 3-4minutes not every
	 * request
	 * 
	 * @return
	 */
	public Date getLastModified() {
		Date dateLastModified;
		dateLastModified = das1Provider.getlastModified();
		System.out.println("last modified from database=" + dateLastModified);
		return dateLastModified;

	}

	public List<DasCoordinateSystem> getCoordinateSystems() {
		return this.getAllCoordinateSystems();
	}

	/**
	 * use a cache of coordinate system objects if there and the database not
	 * modified
	 * 
	 * @return
	 */
	public List<DasCoordinateSystem> getAllCoordinateSystems() {
		Date dateLastModified = getLastModified();
		if (coordinateSystems == null || coordinateSystems.size() == 0
				|| lastUpdateOfCoords == null
				|| (dateLastModified.after(lastUpdateOfCoords))) {
			refreshCoordinates();
		}
		System.out.println("got coordinatesystems");

		return coordinateSystems;
	}

	private void refreshCoordinates() {
		System.out.println("updating coordinates in cache");
		coordinateSystems = das1Provider.getCoordinateSystems();
		lastUpdateOfCoords = new Date(System.currentTimeMillis());
	}

	public DasSource getSingleDasSource(String dasSourceId) {
		List<DasSource> sources = this.getDas1Sources();
		Das1Source emptySource = new Das1Source();
		for (DasSource source : sources) {
			String id = source.getId();
			// System.out.println("id="+id);
			if (id.equals(dasSourceId)) {
				return source;
			}
		}
		System.out.println("no match found returning empty source");
		return emptySource;

	}

	public void refreshAll() {
		this.refreshDasSources();
		this.refreshCoordinates();
	}

}
